package com.ihunanren.common.io;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.List;

import com.ihunanren.common.io.excel.Excel;
import com.ihunanren.common.io.excel.ExcelCell;
import com.ihunanren.common.io.excel.ExcelRow;

/**
 * @author kalman03
 */
public class Test {

	public static void main(String[] args) {
		try {
			ExcelReader excelReader = new ExcelReader("d:/Book1.xls");
			Excel excel = excelReader.getExcel();
			List<ExcelRow> rows = excel.getRows();
			for (ExcelRow row : rows) {
				List<ExcelCell> list = row.getCells();
				String title = list.get(0).getValue();
				String content = list.get(1).getValue();
				inertDb(title, content);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void inertDb(String title, String content) {
		try {
			String driver = "com.mysql.jdbc.Driver";
			// URL指向要访问的数据库名scutcs

			String url = "jdbc:mysql://127.0.0.1:3306/scutcs";

			// MySQL配置时的用户名

			String user = "root";

			// Java连接MySQL配置时的密码

			String password = "nanjiang.jx?";

			// 加载驱动程序

			Class.forName(driver);

			// 连续数据库

			Connection conn = DriverManager.getConnection(url, user, password);

			if (!conn.isClosed())

				System.out.println("Succeeded connecting to the Database!");

			// statement用来执行SQL语句

			Statement statement = conn.createStatement();

			// 要执行的SQL语句

			String sql = "insert into reply_joke(title,content) values(?,?)";
			sql = String.format(sql, title,content);
			statement.executeUpdate(sql);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
